/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.imagelettericon;

import com.github.akashohos90.imageletter.drawable.RoundedDrawable;
import com.github.akashohos90.imageletter.drawable.TextDrawable;
import com.imagelettericon.util.PixelMapUtils;
import com.imagelettericon.util.TextUtils;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.media.image.PixelMap;

import java.security.SecureRandom;

/**
 * Contacts Fragment
 *
 * @since 2021-05-27
 */
public class AltCountriesFragment extends BaseFraction {
    private TextDrawable.IBuilder mTextDrawableBuilder;
    private RoundedDrawable.IBuilder mImageDrawableBuilder;

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_contacts;
    }

    @Override
    public void initComponent() {
        mTextDrawableBuilder = TextDrawable.builder()
                .beginConfig()
                .withBorder(VALUE_THICKNESS)
                .withBorderColor(Color.BLACK.getValue())
                .fontSize(VALUE_FONT_SIZE)
                .endConfig().round();
        mImageDrawableBuilder = RoundedDrawable.builder()
                .beginConfig()
                .withBorder(VALUE_THICKNESS)
                .withBorderColor(Color.BLACK.getValue())
                .endConfig().round();
        ListContainer list = (ListContainer) mComponent.findComponentById(ResourceTable.Id_lc);
        list.setItemProvider(new SampleItemProvider());
    }

    /**
     * SampleItemProvider
     *
     * @since 2021-05-27
     */
    private class SampleItemProvider extends BaseItemProvider {
        @Override
        public int getCount() {
            return REMARKS.length;
        }

        @Override
        public String getItem(int position) {
            return REMARKS[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_list_item,
                        null, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String item = getItem(position);
            updateCheckedState(holder, item, position);
            holder.getTextView().setText(item);
            return convertView;
        }

        private void updateCheckedState(ViewHolder holder, String item, int position) {
            if ((position % VALUE_TWO) == 0) {
                int resourcePosition = new SecureRandom().nextInt(IMAGE_RESOURCE.length);
                PixelMap pixelMap = PixelMapUtils.getPixelMapFromResource(mContext, IMAGE_RESOURCE[resourcePosition]);
                RoundedDrawable roundedDrawable = mImageDrawableBuilder.build(pixelMap);
                holder.getImageView().addDrawTask(new Component.DrawTask() {
                    @Override
                    public void onDraw(Component component, Canvas canvas) {
                        roundedDrawable.setBounds(new Rect(0, 0,
                                holder.getImageView().getWidth(), holder.getImageView().getHeight()));
                        roundedDrawable.drawToCanvas(canvas);
                    }
                });
            } else {
                int color = Color.getIntColor(MATERIAL_COLORS[mSecureRandom.nextInt(MATERIAL_COLORS.length)]);
                TextDrawable drawable =
                        mTextDrawableBuilder.build(TextUtils.computeText(item, 1), color);
                holder.getImageView().addDrawTask(new Component.DrawTask() {
                    @Override
                    public void onDraw(Component component, Canvas canvas) {
                        drawable.setBounds(new Rect(0, 0,
                                holder.getImageView().getWidth(), holder.getImageView().getHeight()));
                        drawable.drawToCanvas(canvas);
                    }
                });
            }
        }
    }
}